<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/3/19
 * Time: 14:01
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class About extends Base
{
    /**
     * @return mixed|\think\response\Json
     * 关于我们
     */
    public function aboutUs(){
        if(Request::instance()->isPost()){
            $con = input('content');
            $res = Db::table('up_about')->where('id', 2)->update(['content'=>$con]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $content = Db::table('up_about')->where('id', 2)->value('content');
        $this->assign('content', $content);
        return $this->fetch('about/aboutus');
    }
    
    /**
     * @return mixed|\think\response\Json
     * 注册协议
     */
    public function agreement(){
        if(Request::instance()->isPost()){
            $con = input('content');
            $res = Db::table('up_about')->where('id', 1)->update(['content'=>$con]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $content = Db::table('up_about')->where('id', 1)->value('content');
        $this->assign('content', $content);
        return $this->fetch('about/agreement');
    }
    
    /**
     * @return mixed|\think\response\Json
     * 隐私政策
     */
    public function yinSi(){
        if(Request::instance()->isPost()){
            $con = input('content');
            $res = Db::table('up_about')->where('id', 3)->update(['content'=>$con]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $content = Db::table('up_about')->where('id', 3)->value('content');
        $this->assign('content', $content);
        return $this->fetch('about/yinsi');
    }
    
    /**
     * 热门城市
     */
    public function hotcitys(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            $map['level'] = 2;
            if(isset($user_gender)&&$user_gender != ""){
                $map['hot'] = $user_gender;
            }
            if(isset($key)&&$key!="")
            {
                $map['district'] = ['like',"%" . $key . "%"];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('area')->where($map)->count();
            $list = Db::name('area')
                ->where($map)
                ->page($page,$limit)
                ->order('hot desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch('about/hot');
    }
    /**
     * 所有城市列表
     */
    public function areas(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($user_gender)&&$user_gender != ""){
                $map['level'] = $user_gender;
            }
            if(isset($key)&&$key!="")
            {
                $map['district'] = ['like',"%" . $key . "%"];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('area')->where($map)->count();
            $list = Db::name('area')
                ->where($map)
                ->page($page,$limit)
                ->order('district_id asc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch('about/areas');
    }
    /**
     * 市添加
     * @return mixed|\think\response\Json
     */
    public function shiadd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            $name = $params['name'];
            $sheng = $params['citys'][0];
            $have = Db::name('area')
                ->where('district',$name)
                ->where('pid',$sheng)
                ->where('level',2)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已经有此名称了']);
            }else{
                $data = [
                    'district'=>$name,
                    'pid'=>$sheng,
                    'level'=>2
                ];
                $res = Db::name('area')->insert($data);
                if($res){
                    $this->updatecache();
                    return json(['code' => 200, 'msg' => '新增成功']);
                }else{
                    return json(['code' => 100, 'msg' => '新增失败']);
                }
            }

        }
        $this->updatecache();
        return $this->fetch('about/shiadd');
    }
    /**
     * 区添加
     * @return mixed|\think\response\Json
     */
    public function quadd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            $name = $params['name'];
            $shi = $params['citys'][1];
            $have = Db::name('area')
                ->where('district',$name)
                ->where('pid',$shi)
                ->where('level',3)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已经有此名称了']);
            }else{
                $data = [
                    'district'=>$name,
                    'pid'=>$shi,
                    'level'=>3
                ];
                $res = Db::name('area')->insert($data);
                if($res){
                    $this->updatecache();
                    return json(['code' => 200, 'msg' => '新增成功']);
                }else{
                    return json(['code' => 100, 'msg' => '新增失败']);
                }
            }

        }
        $this->updatecache();
        return $this->fetch('about/quadd');
    }
    /**
     * 街道添加
     * @return mixed|\think\response\Json
     */
    public function jiedaoadd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            $name = $params['name'];
            $sheng = $params['citys'][0];
            $shi = $params['citys'][1];
            $qu = $params['citys'][2];
            $have = Db::name('area')
                ->where('district',$name)
                ->where('pid',$qu)
                ->where('level',4)
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已经有此街道名称了']);
            }else{
                $data = [
                    'district'=>$name,
                    'pid'=>$qu,
                    'level'=>4
                ];
                $res = Db::name('area')->insert($data);
                if($res){
                    $this->updatecache();
                    return json(['code' => 200, 'msg' => '新增成功']);
                }else{
                    return json(['code' => 100, 'msg' => '新增失败']);
                }
            }

        }
        $this->updatecache();
        return $this->fetch('about/jiedaoadd');
    }
    public function updatecache()
    {
        $area = Db::name('area')
            ->where('level','in','1,2,3')
            ->field(['district_id'=>'id','district'=>'title','pid'])
            ->select ();
        cache('area_data',json_encode ($area));
        return true;
    }
    /***
     * @return \think\response\Json
     * 更新城市json数据
     */
    public function makejsonareas(){
        set_time_limit(0);
        $pro = Db::table('up_area')
            ->where('level',1)
//            ->where('district_id',1001)
            ->field('district_id id,district name')
            ->select();
        $shi = Db::table('up_area')
            ->where('level',2)
            ->field('district_id id,district name,pid parentId')
            ->select();
        $qu = Db::table('up_area')
            ->where('level',3)
            ->field('district_id id,district name,pid parentId')
            ->select();
        $street = Db::table('up_area')
            ->where('level',4)
            ->field('district_id id,district name,pid parentId')
            ->select();
        foreach ($qu as $k=>$v){
            foreach ($street as $kk=>$vv){
                if($vv['parentId'] == $v['id']){
                    $qu[$k]['sub'][] = $vv;
                }
            }
        }
        foreach ($shi as $k=>$v){
            foreach ($qu as $kk=>$vv){
                if($vv['parentId'] == $v['id']){
                    $shi[$k]['sub'][] = $vv;
                }
            }
        }
        foreach($pro as $k=>$v){
            $pro[$k]['parentId'] = 0;
            foreach ($shi as $kk=>$vv){
                if($vv['parentId'] == $v['id']){
                    $pro[$k]['sub'][] = $vv;
                }
            }
        }
        $root = $_SERVER['DOCUMENT_ROOT'];
        $myfile = fopen("$root/static/area.json", "w") or die("Unable to open file!");
        $txt = json_encode($pro);
        fwrite($myfile, $txt);
        fclose($myfile);
        return json(['code' => 200, 'msg' => '更新成功']);
    }
    /***
     * @return \think\response\Json
     * 设置热门
     */
    public function sethot(){
        $user_id = input('id');
        $res = Db::table('up_area')->where('district_id', $user_id)->setField('hot', 2);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 取消热门
     */
    public function cancelhot(){
        $user_id = input('id');
        $res = Db::table('up_area')->where('district_id', $user_id)->setField('hot', 1);
        if($res){
            return json(['code' => 200, 'msg' => "操作成功"]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }


    /***
     * @return \think\response\Json
     * 意见反馈
     */
    public function feedback(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['k_title|k_desc'] = ['like',"%" . $key . "%"];
            }
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('kefu')->where($map)->count();
            $list = Db::name('kefu')
                ->where($map)
                ->page($page,$limit)
                ->order('k_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch('about/feedback');
    }
    
    /***
     * @return \think\response\Json
     * 解决反馈
     */
    public function solveDone(){
        if(Request::instance()->isPost()){
            $user_id = input('k_id');
            $data = [
                'k_status' => 2,
                'k_remark' => input('k_remark')
            ];
            $res = Db::table('up_kefu')->where('k_id', $user_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => "操作成功"]);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $k_id = input('k_id');
        $this->assign('k_id', $k_id);
        return $this->fetch('about/solvedone');
    }
    
    /**
     * 贷款利率
     * @return mixed|\think\response\Json
     */
    public function daik(){
        if(Request::instance()->isPost()){
            $con = input('content');
            $res = Db::table('up_about')->where('id', 4)->update(['content'=>$con]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $content = Db::table('up_about')->where('id', 4)->value('content');
        $this->assign('content', $content);
        return $this->fetch('about/daik');
    }
    
    /**
     * 卖房广告图
     * @return mixed|\think\response\Json
     */
    public function saleImage(){
        if(Request::instance()->isPost()){
            $con = input('content');
            $res = Db::table('up_about')->where('id', 5)->update(['content'=>$con]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $content = Db::table('up_about')->where('id', 5)->value('content');
        $this->assign('content', $content);
        return $this->fetch('about/saleimage');
    }
    
    /**
     * 买房广告图
     * @return mixed|\think\response\Json
     */
    public function buyImage(){
        if(Request::instance()->isPost()){
            $con = input('content');
            $res = Db::table('up_about')->where('id', 6)->update(['content'=>$con]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $content = Db::table('up_about')->where('id', 6)->value('content');
        $this->assign('content', $content);
        return $this->fetch('about/buyimage');
    }
    
    /**
     * 客服电话
     * @return mixed|\think\response\Json
     */
    public function servicePhone(){
        if(Request::instance()->isPost()){
            $con = input('content');
            $con2 = input('content2');
            $res = Db::table('up_about')->where('id', 7)->update(['content'=>$con]);
            $res = Db::table('up_about')->where('id', 8)->update(['content'=>$con2]);
            return json(['code' => 200, 'msg' => '操作成功']);
        }
        $content = Db::table('up_about')->where('id', 7)->value('content');
        $content2 = Db::table('up_about')->where('id', 8)->value('content');
        $this->assign('content', $content);
        $this->assign('content2', $content2);
        return $this->fetch('about/servicephone');
    }
    
}